package com.mzz.structure.proxy.dynamicproxyfactory;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author xushijian
 * @date 18/5/30 下午12:23
 */
public class DynamicProxyFactory<T> {

    @SuppressWarnings("unused")
    private T proxy;

    @SuppressWarnings("unused")
    public T getProxy(final Class<? extends T> clazz) {

        /**
         * 返回获取接口上实现的数组
         */
        Class<?>[] superClazzArray = clazz.getInterfaces();

        //返回代理实例(通过反射实现)
        return (T) (Proxy.newProxyInstance(clazz.getClassLoader(), superClazzArray, new InvocationHandler() {
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                return method.invoke(clazz.newInstance(), args);
            }
        }));
    }
}
